import babelpolyfill from 'babel-polyfill'
import Vue from 'vue'
import App from './App'
import axios from 'axios';
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
//import './assets/theme/theme-green/index.css'
import VueRouter from 'vue-router'
import store from './vuex/store'
import Vuex from 'vuex'
//import NProgress from 'nprogress'
//import 'nprogress/nprogress.css'
import routes from './routes'
// import Mock from './mock'
// Mock.bootstrap();
import 'font-awesome/css/font-awesome.min.css'

Vue.use(ElementUI)
Vue.use(VueRouter)
Vue.use(Vuex)

//NProgress.configure({ showSpinner: false });

const router = new VueRouter({
  routes
})

//判断登录
router.beforeEach((to, from, next) => {
  if (to.path == '/login') {
    sessionStorage.removeItem('user');
  }
  let user = JSON.parse(sessionStorage.getItem('user'));
  if (!user && to.path != '/login') {
    next({ path: '/login' })
  } else {
    next()
  }
})
 
// 添加请求拦截器(在发送请求之前添加请求头信息)
axios.interceptors.request.use(function (config) {
  config.headers['X-Requested-With'] = 'XMLHttpRequest';
  if(sessionStorage.getItem('user')){
     config.headers['Authorization'] = 'Bearer ' + JSON.parse(sessionStorage.getItem('user')).access_token; //根据不同的业务需求设置不同的请求头信息
  }
  return config;
}, function (error) {//请求失败、错误
  return Promise.reject(error);
});

// 添加响应拦截器(请求响应)
axios.interceptors.response.use(function (response) {
  // if(response.status == 200){
  //   this.$message('登录成功');
  // }
  return response;
}, function (error) {
  return Promise.reject(error);
});


new Vue({
  //el: '#app',
  //template: '<App/>',
  router,
  store,
  //components: { App }
  render: h => h(App)
}).$mount('#app')

